package com.lhdx.practical.controllers;

import com.lhdx.practical.bean.Depart;
import com.lhdx.practical.bean.Menu;
import com.lhdx.practical.bean.MenuConfig;
import com.lhdx.practical.bean.Person;
import com.lhdx.practical.service.DepartService;
import com.lhdx.practical.service.MenuConfigService;
import com.lhdx.practical.service.MenuService;
import com.lhdx.practical.service.PersonService;
import com.lhdx.practical.utils.Constants;
import com.lhdx.practical.utils.CookieUtils;
import com.lhdx.practical.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/function")
public class IndexController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private PersonService personService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuConfigService menuConfigService;
    @Autowired
    private DepartService departService;

    @GetMapping("/index")
    public ModelAndView index() {
        long userid = Long.parseLong(CookieUtils.getCookieByNameValue(request, Constants.LOGINED));
        Person person = personService.findById(userid);

        String reurl = "index";
        List<Depart> departs = new ArrayList<>();
        List<Menu> menus = null;
        long gradeid = 0;
        if(person.getType() == 0){
            reurl = "admin";
        }else if(person.getType() == 1){
            reurl = "teacher";
            gradeid = StrUtils.getLong(request,"gradeid");
            departs = departService.findAllByPid(0);
            if(departs.size() > 0 && gradeid == 0){
                gradeid = departs.get(0).getId();
            }
            List<MenuConfig> configs = menuConfigService.findAllByPersonidAndGradeid(person.getId(),gradeid);
            Long[] ids = new Long[configs.size()+4];
            for(int i=0;i<configs.size();i++){
                ids[i] = configs.get(i).getMenuid();
            }
            ids[configs.size()] = 201911151263870021L;
            ids[configs.size()+1] = 201911151263870024L;
            ids[configs.size()+2] = 201911151263870023L;
            ids[configs.size()+3] = 201911151263869991L;
            menus = menuService.findAllByIdIn(ids);
            this.setMenu(menus);
        }else if(person.getType() == 2){
            reurl = "student";
            gradeid = person.getGradeid();
            menus = menuService.findAllByPid(0);
            menus.add(menuService.findById(201911151263870024l));
            this.setMenu(menus);
        }
        Depart depart = new Depart();
        if(gradeid != 0){
            depart = departService.findById(gradeid);
        }
        return new ModelAndView(reurl)
                .addObject("menus",menus)
                .addObject("person",person)
                .addObject("departs",departs)
                .addObject("gradeid",gradeid)
                .addObject("gradename",depart.getDepartname());
    }

    private void setMenu(List<Menu> menus){
        for(Menu menu : menus) {
            if(menu.getId() == 201911151263870024L){
                menu.setMenuList(menuService.findAllByPid(2));
            }else if(menu.getId() == 201911151263870021L) {
                menu.setMenuList(menuService.findAllByPid(3));
            }else if(menu.getId() == 201911151263869991L){
                menu.setMenuList(menuService.findAllByPid(4));
            }else if(menu.getId() == 201911151263870023L){
                menu.setMenuList(menuService.findAllByPid(5));
            }else{
                menu.setMenuList(menuService.findAllByPid(1));
            }


        }
    }
}
